/**************************************************************************//**
\file  windspeed.c

\brief This module is used for measure wind speed value.

\author
	Satxo

\internal
  History:
    21/10/12 R. Rodrigues - Created
******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <windspeed.h>
#include <appTimer.h>
#include <bspTaskManager.h>
#include <types.h>
#include <gpio.h>
#include <irq.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/

#define WINDSPEED_DELAY							10	// in milliseconds

#define WINDSPEED_STATE_QUEUE_LEN				8	// state queue length

/******************************************************************************
                   Global variables section
******************************************************************************/
// The flow control variable
static WindSpeedControl_t windSpeedControl = { .windSpeedData = 0 };

// hods a FIFO queue of task for the bspHIH5031Handler task handler function
static WindSpeedStates_t windSpeedState[WINDSPEED_STATE_QUEUE_LEN] = { WINDSPEED_IDLE };

// Timer structure
static HAL_AppTimer_t windSpeedDelay;

uint16_t value; // Auxiliary measure

void windSpeedDataCallback(void);

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief  Opens the component to use.

\return
  BC_SUCCESS - the component is ready to been use.
  BC_FAIL - otherwise.

******************************************************************************/
result_t openWindSpeed(void)
{
	if (WINDSPEED_IDLE == windSpeedState[0])
	{
		HAL_RegisterIrq(IRQ_7, IRQ_RISING_EDGE, windSpeedDataCallback);
		//windSpeedControl.windSpeedData = 0;
		HAL_EnableIrq(IRQ_7);
	    return BC_SUCCESS;
	}
		
	return BC_FAIL;
}


/**************************************************************************//**
\brief  Performs the test if the component have completed request.

\return
  BC_FAIL - the previous request is not completed.
  BC_SUCCESS - otherwise.
******************************************************************************/
result_t closeWindSpeed(void)
{
  if (WINDSPEED_IDLE == windSpeedState[0])
  {
		//HAL_DisableIrq(IRQ_7);
		//HAL_UnregisterIrq(IRQ_7);
		return BC_SUCCESS;
  }	

  return BC_FAIL;
}

/**************************************************************************//**
\brief Reads data from wind speed sensor.

\param[in]
  f - callback method.
\param[in]
  data - sensor data.
******************************************************************************/
void readWindSpeedData(void (*f)(uint16_t data))
{	
	uint16_t speed = windSpeedControl.windSpeedData;
	windSpeedControl.windSpeedData = 0;
	f(speed);
}

/**************************************************************************//**
\brief  Interrupt increments counter
******************************************************************************/
void windSpeedDataCallback(void)
{
	windSpeedControl.windSpeedData = windSpeedControl.windSpeedData + 1;
	HAL_EnableIrq(IRQ_7);
}

// eof windspeed.c
